package IOService;

import dao.CommodityDao;
import pojo.CommodityList;
import pojo.Commodity;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class DeleteService implements IOService{
    @Override
    public void work(CommodityList commodityList) {
        System.out.println("要删除的物品");
        Scanner sc= new Scanner(System.in);
        String s = sc.next();
        CommodityDao bookDao = new CommodityDao();
        try {
            List<Commodity> show = bookDao.Show();
            Iterator var6 = show.iterator();
            while(var6.hasNext()) {
                Commodity b = (Commodity) var6.next();
                if (b.getName().equals(s)) {
                    int delete = bookDao.Delete(s);
                    if (delete == 1) {
                        System.out.println("删除成功");
                    } else {
                        System.out.println("删除失败");
                    }
                }
            }
        } catch (Exception var9) {
            var9.printStackTrace();
        }

    }
//        int index=-1;
//        for (int i=0;i<commodityList.getCommdityCount();i++) {
//            Commodity commodities=commodityList.getCommodities(i);
//            if (commodities.getName().equals(s)){
//                index=i;
//                break;
//            }
//        }
//        for (int j=index;j<commodityList.getCommdityCount()-1;j++){
//            Commodity commodities=commodityList.getCommodities(j+1);
//            commodityList.setCommodities(j,commodities);
//        }
//        commodityList.setCommdityCount(commodityList.getCommdityCount()-1);
//        System.out.println("删除成功");
//        CommodityDao commodityDao  = new CommodityDao();
//        try {
//            List<Commodity> show = commodityDao.Show();
//            for (Commodity b :show){
//                if (b.getName().equals(s)){
//                    int delete = commodityDao.Delete(s);
//                    if ( delete == 1){
//                        System.out.println("删除成功");
//                    }else{
//                        System.out.println("删除失败");
//                    }
//                }
//            }
//
//    }
}
